package com.hxz.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxz.entity.Article;
import com.hxz.service.ArticleService;
import io.swagger.annotations.Api;

import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by hxz on 2019/11/14
 */
@RestController
@Component
@RequestMapping("/article")
@Api(description = "文章controller,文章的增删改查")
public class ArticleController {

	@Autowired
	ArticleService articleService;
	// 以spring自带控制台方式输出，节省内存，更加整洁
	private static final Logger logger = LoggerFactory.getLogger(ArticleController.class);

	/**
	 * 获取所有文章 这里更改一下时间为yyyy-MM-dd格式
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public PageInfo<Article> list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum)
			throws Exception {
		Integer rows = 4;
		PageHelper.startPage(pageNum, rows);
		List<Article> list = articleService.list();
		PageInfo<Article> pageInfo = new PageInfo<>(list);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String s = sdf.format(new Date());
		for (int i = 0; i < pageInfo.getList().size(); i++) {
			pageInfo.getList().get(i).setDate(s);
		}
		// logger.info("进入ArticleController+++++++++++++++++====================");
		return pageInfo;
	}

	/**
	 * 按类型显示博客
	 */
	@RequestMapping("/column/{category}")
	public String column(@PathVariable("displayname") String dispalyname, @PathVariable("category") String category,
			Model model) {
		model.addAttribute("articles", articleService.getArticleByCategoryName(category));
		model.addAttribute("displayName", dispalyname);

		return "front/columnPage";
	}

	/**
	 * 显示文章详细信息+单个文章评论
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/detail")
	public Map<String, Object> detail(@RequestParam("id") String id, Model model, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		map.put("article", articleService.getById(id));
		return map;
	}

	/**
	 * 搜索文章
	 * 
	 * @param key：文章关键字
	 * @return
	 */
	@RequestMapping("/search")
	public List<Article> search(@Param("key") String key) {
		List<Article> articles = articleService.search(key);
		return articles;
	}

	/**
	 * 获取热门文章
	 * 
	 * @return
	 */
	@RequestMapping("/gethotarticle")
	public List<Article> getHotArticleList() {
		List<Article> hotarticle = articleService.getHotArticleList();
		return hotarticle;
	}
	/**
	 * 
	 */
	@RequestMapping(value="test")
	@ResponseBody
	@Cacheable(value = "aboutUser", key="'user_'+#id") 
	public String  save(@RequestParam(value="id",required=true) Integer id) {
		

		return "dawdaw";
	}
}
